package br.com.escola.hibernate;


import java.util.List;

import javax.persistence.EntityManager;

public class DAO<T>{
   private final EntityManager em;
   private final Class<T> classe;

   public DAO(EntityManager em, Class<T> classe){
      this.em = em;
      this.classe = classe;
   }
   //Metodos de CRUD genericos

   public T busca(Integer id){
      return this.em.getReference(classe, id);
   }

   @SuppressWarnings("unchecked")
   public List<T> lista(){
	   List resultList = em.createQuery("from " + classe.getName()).getResultList();
	   return resultList;
   }

   public void adiciona(T t){
      this.em.persist(t);
   }

   public void remove(T t){
      this.em.remove(t);
   }
}